from scapy.all import *
from scapy.layers.inet import *
import pyshark
import numpy as np

# print(explore(scapy.layers.inet))
# print("------IP------")
# print(explore(scapy.packet.ls(IP)))

pkt = IP(src="192.168.0.2", dst="192.168.32.129") / ICMP()

res = sr1(pkt)
print(res.summary())
print(res.show())
res = sr1(pkt)
wrpcap("test006.cap", pkt)
# 初始化一些用于存储数据的列表
timestamps = []
source_ips = []
destination_ips = []
pcap_file = 'test006.cap'
capture = pyshark.FileCapture(pcap_file)
# 遍历pcap中的每个packet
for packet in capture:
    # 获取时间戳
    timestamp = float(packet.sniff_timestamp)
    timestamps.append(timestamp)

    # 获取源IP和目的IP

    source_ip = packet.ip.src
    print("源IP", packet.ip.src)
    destination_ip = packet.ip.dst
    print("目标IP", packet.ip.dst)
    source_ips.append(source_ip)
    destination_ips.append(destination_ip)

# 将列表转换为NumPy数组
timestamps = np.array(timestamps)
print("timestamps", np.array(timestamps))
source_ips = np.array(source_ips)
print("数组形IP地址", np.array(source_ips))
destination_ips = np.array(destination_ips)
print("数组形IP地址", np.array(destination_ips))
# 现在你可以使用NumPy对数据进行分析了，例如计算平均时间戳
average_timestamp = np.mean(timestamps)

print(f"Average Timestamp: {average_timestamp}")
print(conf.ifaces)


def cell(pkt):
    print(pkt.summary)


sniff(filter="icmp and host 192.168.0.2", iface='Microsoft KM-TEST 环回适配器', prn=cell, count=3)